package com.speed_run.config;

import com.speed_run.mapper.AuthMapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import com.speed_run.model.UserAuth;
import org.springframework.stereotype.Component;

public class UserRealm extends AuthorizingRealm {

	@Autowired
	private AuthMapper mapper;
	
	/**
	 * 登录验证
	 * */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		UserAuth user = mapper.checkUser(token.getPrincipal().toString());
		if(user == null){
			throw new UnknownAccountException(); //用户不存在
		} else if (false){
			throw new LockedAccountException(); //账号锁定
		}
		return new SimpleAuthenticationInfo(user, user.getAuthPwd(),getName());
	}


	/**
	 * 	 授权
	 * @param principals
	 * @return
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) {
		// TODO Auto-generated method stub
		return null;
	}

}
